package cn.b.sky.mes.mm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.mm.dao.InventoryAlarmRecipientDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.mm.model.InventoryAlarmRecipient;
import cn.b.sky.mes.mm.model.Tree;
import cn.b.sky.mes.mm.model.condition.InventoryAlarmRecipientCondition;
import cn.b.sky.mes.mm.service.InventoryAlarmRecipientService;
import cn.b.sky.mes.mm.service.SafetyinventroyService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.message.model.MessageUser;
import cn.b.sky.message.service.MessageService;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.service.model.User;

@Service
public class InventoryAlarmRecipientServiceImpl implements InventoryAlarmRecipientService {

  private static final Logger LOG = LoggerFactory
      .getLogger(InventoryAlarmRecipientServiceImpl.class);

  @Autowired
  InventoryAlarmRecipientDao inventoryAlarmRecipientDao;

  @Resource
  SafetyinventroyService safetyinventroyService;

  @Resource
  MessageService messageService;

  @Override
  public List<InventoryAlarmRecipient> selectInventoryAlarmRecipientListByCondition(
      InventoryAlarmRecipientCondition condition) {
    return inventoryAlarmRecipientDao.selectInventoryAlarmRecipientListByCondition(condition);
  }

  @Override
  public int selectInventoryAlarmRecipientCountByCondition(
      InventoryAlarmRecipientCondition condition) {
    return inventoryAlarmRecipientDao.selectInventoryAlarmRecipientCountByCondition(condition);
  }

  @Override
  public List<InventoryAlarmRecipient> getInventoryAlarmRecipient(String siteCode, String userCode,
      String storageLocationCode, String materialCode, boolean hasWarehouse, boolean hasMaterialType) {
    return inventoryAlarmRecipientDao.getInventoryAlarmRecipient(siteCode, userCode,
        storageLocationCode, materialCode, hasWarehouse, hasMaterialType);
  }

  @Override
  public void insertInventoryAlarmRecipient(InventoryAlarmRecipient recipient) {
    inventoryAlarmRecipientDao.insertInventoryAlarmRecipient(recipient);
  }

  @Override
  public void updateInventoryAlarmRecipient(InventoryAlarmRecipient recipient) {
    inventoryAlarmRecipientDao.updateInventoryAlarmRecipient(recipient);
  }

  @Override
  public BatchJobReport deleteInventoryAlarmRecipient(Long id) {
    String message = "";
    boolean success = true;
    String cause = "";

    try {
      inventoryAlarmRecipientDao.deleteInventoryAlarmRecipientById(id);
    } catch (Exception e) {
      LOG.error("{}", e);
      success = false;
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(e.getMessage())) {
        // 外键删除已存在提示
        errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
            SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                SecurityContextUtil.getLanguageCode(), "notDelete.M");
      }
      cause = errorMsg;
    }
    return new BatchJobReport(message, cause, success, SecurityContextUtil.getLanguageCode(),
        SecurityContextUtil.getSiteCode());
  }

  @Override
  public void deleteInventoryAlarmRecipient(String siteCode, String userCode,
      String storageLocationCode, String materialCode, boolean hasWarehouse, boolean hasMaterialType) {
    inventoryAlarmRecipientDao.deleteInventoryAlarmRecipient(siteCode, userCode,
        storageLocationCode, materialCode, hasWarehouse, hasMaterialType);
  }

  @Override
  public List<Tree> selectMaterialTypeTrees(String siteCode) {
    return inventoryAlarmRecipientDao.selectMaterialTypeTrees(siteCode);
  }

  @Override
  public List<Tree> selectWarehouseTrees(String siteCode) {
    return inventoryAlarmRecipientDao.selectWarehouseTrees(siteCode);
  }

  @Override
  public void doSendAlarm(String siteCode, String storageLocationCode, String warehouseCode,
      String materialTypeCode, String materialCode, boolean isLocationSafetyInventroy) {
    InventoryAlarmRecipient alarmRecipient = new InventoryAlarmRecipient();
    alarmRecipient.setSiteCode(siteCode);
    alarmRecipient.setStorageLocationCode(storageLocationCode);
    alarmRecipient.setWarehouseCode(warehouseCode);
    alarmRecipient.setMaterialCode(materialCode);
    alarmRecipient.setMaterialTypeCode(materialTypeCode);
    List<User> users = inventoryAlarmRecipientDao.selectAlarmUsers(alarmRecipient);
    if (users != null) {

      String message = null;
      if (!isLocationSafetyInventroy) {

        message = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
            SecurityContextUtil.getLanguageCode(), "warehouseInventoryAlarm.M", warehouseCode,
            materialCode);
      } else {

        message = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
            SecurityContextUtil.getLanguageCode(), "storageLocationInventoryAlarm.M",
            warehouseCode, storageLocationCode, materialCode);
      }
      String[] userList = new String[users.size()];
      for (int i = 0; i < users.size(); i++) {
        userList[i] = users.get(i).getUserCode();
      }
      MessageUser messageUser = new MessageUser();
      messageUser.setUserList(userList);
      messageService.sentMessage(siteCode, message, messageUser);
    }

  }

}
